<?php
require_once(UHU_LIBS .'/Smarty/Smarty.class.php');
class Smarty_Registry
{
	private static $objs = array();
	private static $cache_key;
	final static function &get($template_dir,$compile_dir,$cache_dir)
	{
		if(self::exists($template_dir,$compile_dir,$cache_dir) === false)
		{
			self::add($template_dir,$compile_dir,$cache_dir);
		}
		return self::$objs[self::$cache_key];
	}
	private static function add($template_dir,$compile_dir,$cache_dir)
	{
		try
		{
			$obj = new Smarty_Machine($template_dir,$compile_dir,$cache_dir);
			$obj->assign('errors',array());
			$obj->assign('messages',array());
		}
		catch(Exception $e)
		{
			headerBySAPI('HTTP/1.1 500 Internal Server Error');
			if(defined('UHU4_DEBUG_MODE'))
			{
				die((string)$e);
			}
			else
			{
				die('Could not instantiate Smarty');
			}
		}
		self::$objs[self::$cache_key] = $obj;
	}
	private static function cache_key($template_dir,$compile_dir,$cache_dir)
	{
		self::$cache_key = md5($template_dir . $compile_dir . $cache_dir);
		return self::$cache_key;
	}
	public static function exists($template_dir,$compile_dir,$cache_dir)
	{
		return (array_key_exists(self::cache_key($template_dir,$compile_dir,$cache_dir),self::$objs) && self::$objs instanceof Smarty);
	}
}
class Smarty_Machine extends Smarty
{
	public function __construct($template_dir,$compile_dir,$cache_dir)
	{
		if(is_dir($template_dir) === false)
		{
			if(mkdir($template_dir,0750,true) === false)
			{
				throw new Exception('Could not create Smarty Template directory:' . $template_dir,E_ERROR);
			}
		}
		$this->template_dir    = $template_dir;
		if(is_dir($compile_dir) === false)
		{
			if(mkdir($compile_dir,0750,true) === false)
			{
				throw new Exception('Could not create Smarty Compile directory:' . $template_dir,E_ERROR);
			}
		}
		$this->compile_dir    = $compile_dir;
		if(is_dir($cache_dir) === false)
		{
			if(mkdir($cache_dir,0750,true) === false)
			{
				throw new Exception('Could not create Smarty Cache directory:' . $template_dir,E_ERROR);
			}
		}
		$this->cache_dir    = $cache_dir;
	}
	public function addError($str)
	{
		parent::append('errors',$str);
	}
	public function addMessage($str)
	{
		parent::append('messages',$str);
	}
	public function _display($filename=null,$cache_id=null,$compile_id=null)
	{
		parent::display($filename=null,$cache_id=null,$compile_id=null);
	}
	public function display($filename=null,$cache_id=null,$compile_id=null)
	{
		if(is_file($this->template_dir . $filename) === false)
		{
			switch(defined('UHU4_DEBUG_MODE'))
			{
				case true:
					throw new Exception('Could not locate file: ' . $this->template_dir . $filename,E_ERROR);
				break;
				default:
					throw new Exception('Could not generate page');
				break;
			}
		}
		$doc = parent::fetch($filename,$cache_id,$compile_id);
		if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'],'gzip'))
		{
			$doc = gzencode($doc,9);
			header('Content-Encoding:gzip');
		}
		header('Content-Length:' . strlen($doc));
		return print($doc);
	}
}
?>